const Router = require("koa-router")
const glob = require('glob')
const { r } = require('path')
const _ = require('lodash')

const symbolPrefix = Symbol('prefix')
const routerMap = new map()
const isArray = c => _.isArray(c) ? c : [c]

export class Route {
    constructor (app,apiPath) {
        this.app = app
        this.apiPath =apiPath
        this.router = new Router()
    }

    init () {
        glob.sync(r(this.apiPath,"./**/*.js")).forEach(require)
        for (let [conf,controller] of routerMap) {
            const controllers = isArray(controller)
            const prefixPath = conf.target[symbolPrefix]
            if (prefixPath) prefixPath = normalizePath(prefixPath)
            const routerPath = prefixPath + conf.path
            this.router[conf.method](routerPath,...controllers)
        }
        this.app.use(tihs.router.routes())
        this.app.use(tihs.router.all())
    }
}

const normalizePath = path => path.startWith("/") ? path : `/${path}`

const router = conf => (target,key,descriptor) => {
    conf.path = normalizePath(conf.path)
    routerMap.set({
        target: target,
        ...conf,
    },target[key])
}

export const controller = path => target => (target.prototype[symbolPrefix] = path)

export const get = path => router({
    method : 'get',
    path : path
})
export const post = path => router({
    method : 'post',
    path : path
})
export const put = path => router({
    method : 'put',
    path : path
})
export const del = path => router({
    method : 'del',
    path : path
})
export const use = path => router({
    method : 'use',
    path : path
})
export const all = path => router({
    method : 'all',
    path : path
})